package demo06.threadSingleton;

/**
 * 双重检查加锁
 * 多线程安全的单利模式
 * */
public class ThreadSingleton {
	//volatile关键字确保被多线程共享
	private volatile static ThreadSingleton uniqueInstance;//懒汉式只有用到时才创建实例
	
	//私有的构造函数
	private ThreadSingleton() {
		
	}
	
	public static ThreadSingleton getUniqueInstance() {
		if(uniqueInstance==null){//检查实例不存在就进入同步快
			synchronized(ThreadSingleton.class){//线程锁
				if(uniqueInstance==null){//在检查一次如果还是null,就创建
					uniqueInstance = new ThreadSingleton();
				}
			}
		} 
		return uniqueInstance;
	}

}
